package com.it.tydic.estate.module.controller.common;

import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.module.model.inner.TbDictionary;
import com.it.tydic.estate.module.model.inner.TbResource;
import com.it.tydic.estate.module.model.inner.TbUser;
import com.it.tydic.estate.module.service.ResourceService;
import com.it.tydic.estate.module.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.util.StringUtil;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("resource")
@Api(tags = "资源菜单接口")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;




    @GetMapping("/initMenu")
    @ApiOperation(value = "查询菜单", notes = "根据用户查询菜单")
    public Result initMenu() { //@PathVariable 获取/get/{id}中id
        return Result.success(resourceService.initMenu());
    }



    @GetMapping("/findById/{id}")
    @ApiOperation(value = "根据id查询", notes = "资源菜单")
    @ApiImplicitParam(name = "id", value = "id",  paramType = "path", required = true, dataType =  "String")
    public Result findById(@PathVariable String id) { //@PathVariable 获取/get/{id}中id
        if(StringUtil.isEmpty(id)){
            Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        return Result.success( resourceService.findById(id));
    }


    @DeleteMapping("/deleteById/{id}")
    @ApiOperation(value = "根据id删除", notes = "资源菜单")
    @ApiImplicitParam(name = "id", value = "id",  paramType = "path", required = true, dataType =  "String")
    public Result deleteById(@PathVariable String id) { //@PathVariable 获取/get/{id}中id
        if(StringUtil.isEmpty(id)){
            Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        boolean flag=resourceService.resourceIsUseById(id);
        if(!flag){
            return Result.success(resourceService.deleteById(id));
        }else{
            return Result.error(CodeMsg.RESOURCE_USE);
        }

    }

    /**
     * 批量删除
     *
     * @param idList 主键集合
     * @return
     */
    @PostMapping("/deleteByIds")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    public Result deleteByIds(@RequestBody List<String> idList) {
        //参数验证
        if(idList == null || idList.size() == 0){
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        boolean flag=resourceService.resourceIsUseByList(idList);
        if(!flag){
            int count=resourceService.deleteByIds(idList);
            if(count == idList.size()){
                return Result.success();
            }
        }else{
            return Result.error(CodeMsg.RESOURCE_USE);
        }

        return Result.error(CodeMsg.DELETE_ERROR);
    }

    /**
     * 新增或修改
     *
     * @param tbResource 资源菜单
     * @return
     */
    @PostMapping("/addOrUpdate")
    @ApiOperation(value = "保存或修改", notes = "资源菜单")
    public Result addOrUpdate(@RequestBody TbResource tbResource) {
        //参数验证
        if(tbResource == null){
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        int count=resourceService.addOrUpdate(tbResource);
        if(count == 1){
            return Result.success();
        }else if(count == -1){
            return Result.error(CodeMsg.DATA_REPEATE);
        }
        return Result.error(CodeMsg.INTER_ERROR);
    }
}
